/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameSekretariat;

import Database.CheckConnection;
import Database.CreateCache;
import Database.HapusCache;
import FrameAdmin.DaftarUser;
import MainUtama.AdminFrameUtama;
import Model.*;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author WIN7
 */
public class SekretariatFrameUtama extends JFrame {

    private JMenuBar menuBar;
    private JMenu berkas, surat, dokumen, sekre;
    private JMenuItem lhtsrt, tmbhsrt, lhtdok, tmbhdok, tambahsekre, daftarUser, close, profile;
    private ArrayList<UserPenerima> dataPenerima;
    private ArrayList<Surat> dataSurat;
    private ArrayList<Dokumen> dataDokumen;

    public SekretariatFrameUtama() throws HeadlessException {
        this.setTitle("Sekretariat USD");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setBounds(350, 200, 500, 400);
        this.setLayout(null);

        menuBar = new JMenuBar();
        menuBar.setSize(500, 30);
        this.add(menuBar);

        sekre = new JMenu("Menu");
        sekre.setSize(70, 30);
        menuBar.add(sekre);

        profile = new JMenuItem("Profile");
        profile.setSize(70, 30);
        sekre.add(profile);
        profile.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    SekreProfile profil = new SekreProfile();
                    profil.setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        tambahsekre = new JMenuItem("Tambah User Penerima");
        tambahsekre.setSize(70, 30);
        sekre.add(tambahsekre);
        tambahsekre.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Sekretambahuser tmbhuser = new Sekretambahuser();
                tmbhuser.setVisible(true);
            }
        });

        daftarUser = new JMenuItem("Daftar User Penerima");
        daftarUser.setSize(70, 30);
        sekre.add(daftarUser);
        daftarUser.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dataPenerima = new ArrayList<UserPenerima>();
                Connection con = CheckConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "12345");
                Statement stat = null;
                try {
                    stat = con.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                String sql = "select * from USER_PENERIMA";
                ResultSet hasil = null;
                try {
                    hasil = stat.executeQuery(sql);
                } catch (SQLException ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    while (hasil.next()) {
                        UserPenerima penerima = new UserPenerima();
                        try {
                            penerima.setIdUserPenerima(hasil.getString("ID_USER"));
                            penerima.setNamaUserPenerima(hasil.getString("NAMA_USER"));
                            dataPenerima.add(penerima);
                        } catch (Exception ex) {
                            Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                FrameDaftarUser daftar = new FrameDaftarUser(dataPenerima);
                daftar.setVisible(true);
            }
        });

        close = new JMenuItem("Close & Logout");
        close.setSize(70, 30);
        sekre.add(close);
        close.addActionListener(new ActionListener() {

            private String[] args;

            @Override
            public void actionPerformed(ActionEvent e) {
                HapusCache.main(args);
                CreateCache.main(args);
                setVisible(false);
                JOptionPane.showMessageDialog(null, "Anda telah Logout");
            }
        });

        berkas = new JMenu("Berkas");
        berkas.setSize(70, 30);
        menuBar.add(berkas);

        surat = new JMenu("Surat");
        surat.setSize(70, 30);
        berkas.add(surat);

        tmbhsrt = new JMenuItem("Tambah Surat");
        tmbhsrt.setSize(70, 30);
        surat.add(tmbhsrt);
        tmbhsrt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SekreTambahSurat tmbhsrt = new SekreTambahSurat();
                tmbhsrt.setVisible(true);
            }
        });

        lhtsrt = new JMenuItem("Daftar Surat");
        lhtsrt.setSize(70, 30);
        surat.add(lhtsrt);
        lhtsrt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dataSurat = new ArrayList<Surat>();
                Connection con = CheckConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "12345");
                Statement stat = null;
                try {
                    stat = con.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                String sql = "select * from SURAT";
                ResultSet hasil = null;
                try {
                    hasil = stat.executeQuery(sql);
                } catch (SQLException ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    while (hasil.next()) {
                        Surat surat = new Surat();
                        try {
                            surat.setNomor_surat(hasil.getString("NO_SURAT"));
                            surat.setPerihal(hasil.getString("PERIHAL"));
                            surat.setAsal_surat(hasil.getString("ASAL_SURAT"));
                            surat.setPenerima_surat(hasil.getString("PENERIMA_SURAT"));
                            surat.setId_penerima(hasil.getString("ID_PENERIMA"));
                            surat.setLetak_surat(hasil.getString("LETAK_SURAT"));
                            surat.setPetugas_penerima(hasil.getString("PETUGAS_PENERIMA"));
                            surat.setTanggal_terima(hasil.getString("TANGGAL_TERIMA"));
                            surat.setTerima(hasil.getString("DAFTAR_TERIMA"));
                            dataSurat.add(surat);
                        } catch (Exception ex) {
                            Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                SekreLihatSurat lhtsrt = new SekreLihatSurat(dataSurat);
                lhtsrt.setVisible(true);
            }
        });

        dokumen = new JMenu("Dokumen");
        dokumen.setSize(70, 30);
        berkas.add(dokumen);

        tmbhdok = new JMenuItem("Tambah Dokumen");
        tmbhdok.setSize(70, 30);
        dokumen.add(tmbhdok);
        tmbhdok.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SekreTambahDokumen tmbhdok = new SekreTambahDokumen();
                tmbhdok.setVisible(true);
            }
        });

        lhtdok = new JMenuItem("Daftar Dokumen");
        lhtdok.setSize(70, 30);
        dokumen.add(lhtdok);
        lhtdok.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dataDokumen = new ArrayList<Dokumen>();
                Connection con = CheckConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "12345");
                Statement stat = null;
                try {
                    stat = con.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                String sql = "select * from DOKUMEN";
                ResultSet hasil = null;
                try {
                    hasil = stat.executeQuery(sql);
                } catch (SQLException ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    while (hasil.next()) {
                        Dokumen dokumen = new Dokumen();
                        try {
                            dokumen.setNomor_dokumen(hasil.getString("NO_DOKUMEN"));
                            dokumen.setPerihal(hasil.getString("PERIHAL"));
                            dokumen.setAsal_dokumen(hasil.getString("ASAL_DOKUMEN"));
                            dokumen.setPenerima_dokumen(hasil.getString("PENERIMA_DOKUMEN"));
                            dokumen.setId_penerima(hasil.getString("ID_PENERIMA"));
                            dokumen.setLetak_dokumen(hasil.getString("LETAK_DOKUMEN"));
                            dokumen.setPetugas_penerima(hasil.getString("PETUGAS_PENERIMA"));
                            dokumen.setTanggal_terima(hasil.getString("TANGGAL_TERIMA"));
                            dokumen.setTerima(hasil.getString("DAFTAR_TERIMA"));
                            dataDokumen.add(dokumen);
                        } catch (Exception ex) {
                            Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(SekretariatFrameUtama.class.getName()).log(Level.SEVERE, null, ex);
                }
                SekreLihatDokumen lhtdok = new SekreLihatDokumen(dataDokumen);
                lhtdok.setVisible(true);
            }
        });
    }
}
